package com.xiyue.leaspring.datasourceTest;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.xiyue.leaspring.vo.News;

@ContextConfiguration(locations = {"classpath:spring/spring-jdbc7.xml"})
@RunWith(SpringJUnit4ClassRunner.class)//设置要测试的工具
public class DataSourceTest7 {
	
	private Logger logger = LoggerFactory.getLogger(DataSourceTest7.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
		
	@Test
	public void testDataSource() throws Exception {
		
		String sql = "INSERT INTO news(title,pubdate,note,price,readcount)VALUES(?,?,?,?,?)";
		List<News> newsList = new ArrayList<News>();
		for(int i=0;i<1000;i++) {
			News vo = new News();
			vo.setTitle("大山脚下的小屋吧"+i);
			vo.setNote("时间即能量完事万物皆对象"+i);
			vo.setPrice(Math.random()*1000);
			vo.setPubDate(new Date());
			vo.setReadCount((int) (Math.random()*1000));
			newsList.add(vo);
		}
		int len[][] = this.jdbcTemplate.batchUpdate(sql, newsList, newsList.size(), new ParameterizedPreparedStatementSetter<News>() {

			@Override
			public void setValues(PreparedStatement ps, News vo) throws SQLException {
				ps.setString(1,vo.getTitle());
				ps.setDate(2,new java.sql.Date(vo.getPubDate().getTime()));
				ps.setString(3,vo.getNote());
				ps.setDouble(4,vo.getPrice());
				ps.setInt(5,vo.getReadCount());
			}
		});
		this.logger.info("更新行数："+len.length);
		
	}

}
